﻿using DryWCS.Model.WMS;
using DryWCS.Service.DbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.Service.WMS
{
    //  public class WareHouseCellInfoService : WMSDbContext<WareHouseCellInfoModel>
    public class WareHouseCellInfoService : LocalDbContext<WareHouseCellInfoModel>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="laneNo">巷道</param>
        /// <param name="lineNo">排</param>
        /// <param name="colNo">列</param>
        /// <param name="layerNo">层</param>
        /// <returns></returns>
        public WareHouseCellInfoModel GetBy(int laneNo, int lineNo, int colNo, int layerNo)
        {
            try
            {
                return Db.Queryable<WareHouseCellInfoModel>().Where(p => p.ZONE_ID == 1377 && p.LANE_NO == laneNo && p.LINE_NO == lineNo && p.COL_NO == colNo && p.LAYER_NO == layerNo && p.POS_TYPE_CODE == "Automated Position" && p.ACTIVE_FLAG)?.First();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="laneNo">巷道</param>
        /// <returns></returns>
        public List<WareHouseCellInfoModel> GetBy(int lineNo)
        {
            try
            {
                return Db.Queryable<WareHouseCellInfoModel>().Where(p => p.ZONE_ID == 1377 && p.LINE_NO == lineNo && p.POS_TYPE_CODE == "Automated Position" && p.ACTIVE_FLAG).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="laneNo">巷道</param>
        /// <param name="lineNo">排</param>
        /// <returns></returns>
        public List<WareHouseCellInfoModel> GetBy(int laneNo, int lineNo)
        {
            try
            {
                return Db.Queryable<WareHouseCellInfoModel>().Where(p => p.ZONE_ID == 1377 && p.LANE_NO == laneNo && p.LINE_NO == lineNo && p.POS_TYPE_CODE == "Automated Position" && p.ACTIVE_FLAG).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int UpdateByCell(int laneNo, int lineNo, int colNo, int layerNo, int status)
        {
            try
            {
                WareHouseCellInfoModel model = GetBy(laneNo, lineNo, colNo, layerNo);
                if (model == null)
                {
                    return -1;
                }
                model.POS_STATUS = status;
                model.VERSION++;
                return Db.Updateable<WareHouseCellInfoModel>(model).IgnoreColumns("UPDATE_TIME_WCS").ExecuteCommand();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
